package com.csxy.chy.controller;

import com.csxy.chy.JwtUtils;
import com.csxy.chy.R;
import com.csxy.chy.entity.UcenterMember;
import com.csxy.chy.entity.vo.LongVo;
import com.csxy.chy.entity.vo.RegisterVo;
import com.csxy.chy.entity.vo.UcenterMemberVo;
import com.csxy.chy.exception.MyException;
import com.csxy.chy.service.UcenterService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class UcenterController {
    @Autowired
    private UcenterService ucenterService;

    // 新路径：/ucenter/login
    @PostMapping("/ucenter/login")
    public R loginNew(@RequestBody LongVo longVo){
        String token = ucenterService.login(longVo);
        return R.ok().data("token",token);
    }
    // 兼容老路径：/serviceUcenter/ucenter/login
    @PostMapping("/serviceUcenter/ucenter/login")
    public R login(@RequestBody LongVo longVo){
        String token = ucenterService.login(longVo);
        return R.ok().data("token",token);
    }
    // 新路径：/ucenter/register
    @PostMapping("/ucenter/register")
    public R registerNew(@RequestBody RegisterVo registerVo){
        ucenterService.register(registerVo);
        return R.ok();
    }
    // 兼容老路径：/serviceUcenter/ucenter/register
    @PostMapping("/serviceUcenter/ucenter/register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterService.register(registerVo);
        return R.ok();
    }
    // 新路径：/ucenter/getUserInfoForJwt/
    @GetMapping("/ucenter/getUserInfoForJwt/")
    public R getUserInfoForJwtNew(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterService.getById(memberId);
        return R.ok().data("userInfo", member);
    }
    // 兼容老路径：/serviceUcenter/ucenter/getUserInfoForJwt/
    @GetMapping("/serviceUcenter/ucenter/getUserInfoForJwt/")
    public R getUserInfoForJwt(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterService.getById(memberId);
        return R.ok().data("userInfo", member);
    }
    // 新路径：/ucenter/getMemberInfoById/{memberId}
    @PostMapping("/ucenter/getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoByIdNew(@PathVariable String memberId){
        UcenterMember member = ucenterService.getById(memberId);
        if (member == null) {
            throw new MyException(20001, "用户不存在");
        }
        UcenterMemberVo memberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member, memberVo);
        return memberVo;
    }
    // 兼容老路径：/serviceUcenter/ucenter/getMemberInfoById/{memberId}
    @PostMapping("/serviceUcenter/ucenter/getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoById(@PathVariable String memberId){
        UcenterMember member = ucenterService.getById(memberId);
        if (member == null) {
            throw new MyException(20001, "用户不存在");
        }
        UcenterMemberVo memberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member, memberVo);
        return memberVo;
    }
    // 新路径：/ucenter/countRegister/{day}
    @GetMapping("/ucenter/countRegister/{day}")
    public R countRegisterNew(@PathVariable String day) {
        Integer count = ucenterService.getCountRegister(day);
        return R.ok().data("countRegister", count);
    }
    // 兼容老路径：/serviceUcenter/ucenter/countRegister/{day}
    @GetMapping("/serviceUcenter/ucenter/countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = ucenterService.getCountRegister(day);
        return R.ok().data("countRegister", count);
    }
}
